package View;

import Controller.StarClusterController;
import Model.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.read.biff.BiffException;

public class FileParser {

	public static void parseXLS(String filename, StarClusterController set){
		try{
			Workbook workbook = Workbook.getWorkbook(new File(filename));
			Sheet sheet = workbook.getSheet(0);

            int attri = sheet.getColumns();
            int entites = sheet.getRows()-1;

            for (int i=0; i<attri; i++)
            {
            	Cell a = sheet.getCell(i,0);
            	String stringa = a.getContents();
            	Cell a1 = sheet.getCell(i,1);
                //détermine le type d'attribut
  		       if (a1.getType() == CellType.LABEL)
  			     set.addAttribute(stringa,AttributeType.TextualDiscreet);
  		       else
			     set.addAttribute(stringa,AttributeType.Numerical);

            }

            ArrayList<AttributeValue> attributeList;
            AttributeValue attribut;

			for (int j=1; j<=entites; j++)
			{
                set.addAction();
				//attributeList = new ArrayList<AttributeValue>();
			   for (int i=0; i < attri; i++ ){
                    Cell a1 = sheet.getCell(i,j);
                    String stringa1;
                    if (a1.getType() == CellType.LABEL){
                        LabelCell lc = (LabelCell) a1;
                        stringa1 = lc.getString();
                    }
                    else{
                        NumberCell nc=(NumberCell) a1;
                        stringa1 = Double.toString(nc.getValue());
                    }
                    set.setValue(j-1,i,stringa1);
			   }
		  }
			/*for (int i= 0; i<entites;i++)
			{
			Action act = new Action();
              act = set.actionList.get(i);
               System.out.println(act.getStringValue(set.getAttribute(0)));
			}*/
		   workbook.close();
		  }
		  catch(IOException e){
				JOptionPane.showMessageDialog(null,
                "Error while opening "+filename+".",
                "Error",
                JOptionPane.ERROR_MESSAGE);
		  }
		  catch(BiffException e ) {
                JOptionPane.showMessageDialog(null,
                "Error while opening "+filename+".",
                "Error",
                JOptionPane.ERROR_MESSAGE);
          }

		 }

}
